/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Change_pass.java
 *
 * Created on Sep 17, 2012, 9:50:53 PM
 */
package Admin;

import ConnnectData.DataConnect;
import Encript.Encripts;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author HP
 */
public class Change_pass extends javax.swing.JFrame {

    /** Creates new form Change_pass */
    Connection con;
    ResultSet result;
    PreparedStatement pre;
    Statement sta;
    String Username;
    

    public Change_pass() {
        initComponents();
        setLocationRelativeTo(null);
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Change_pass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Change_pass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Change_pass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Change_pass.class.getName()).log(Level.SEVERE, null, ex);
        }
        lblConfimPass.setText("");
        lblCurrPass.setText("");
        lblMs.setText("");
        lblNewPass.setText("");
        try{
            con = DataConnect.connectDB();
            sta = con.createStatement();
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public void Change_Pass()
    {
        if(txtCurrPass.getText().equals(""))
        {
            lblCurrPass.setText("Current pass not null !");
            txtCurrPass.requestFocus();
            return;
        }
        if(txtCurrPass.getText().length() > 15)
        {
            lblCurrPass.setText("Current pass not validate !");
            return;
        }
        if(txtNewPass.getText().equals(""))
        {
            lblNewPass.setText("new pass not null !");
            txtNewPass.requestFocus();
            return;
        }
        if(txtNewPass.getText().length() > 15)
        {
            lblNewPass.setText("new pass must < 15 character !");
            txtNewPass.requestFocus();
            return;
        }
        if(txtConfimPass.getText().equals(""))
        {
            lblConfimPass.setText("Cofilm pass not null !");
            txtConfimPass.requestFocus();
            return;
        }
        if(!txtConfimPass.getText().equals(txtNewPass.getText().trim()))
        {
            lblConfimPass.setText("Password not the same !");
            txtConfimPass.requestFocus();
            return;
        }
        String pass = Encripts.encript(txtNewPass.getText().trim());
        try{
            sta.executeQuery("Select * from Staff");
            pre = con.prepareStatement("Update Staff set Staff_Password=? where Staff_Permission = 'Admin'");
            pre.setString(1, pass);
            pre.executeUpdate();
            this.dispose();
            JOptionPane.showMessageDialog(this, "Change successful password ");
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblCurrPass = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblNewPass = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        lblConfimPass = new javax.swing.JLabel();
        lblMs = new javax.swing.JLabel();
        txtNewPass = new javax.swing.JPasswordField();
        txtConfimPass = new javax.swing.JPasswordField();
        txtCurrPass = new javax.swing.JPasswordField();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnChange = new javax.swing.JMenu();
        mnClose = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Current password :");

        jLabel2.setText("*");

        lblCurrPass.setText("jLabel3");

        jLabel3.setText("New password :");

        jLabel4.setText("*");

        lblNewPass.setText("jLabel5");

        jLabel5.setText("Cofilm new password :");

        jLabel6.setText("*");

        lblConfimPass.setText("jLabel7");

        lblMs.setText("jLabel7");

        mnChange.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/edit.JPG"))); // NOI18N
        mnChange.setText("Change PassWord");
        mnChange.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnChangeMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnChange);

        mnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/exit.png"))); // NOI18N
        mnClose.setText("Close");
        mnClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnCloseMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnClose);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblMs)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNewPass, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                            .addComponent(txtConfimPass, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                            .addComponent(txtCurrPass, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(lblCurrPass))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(lblNewPass))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(18, 18, 18)
                                .addComponent(lblConfimPass)))
                        .addGap(238, 238, 238))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(lblCurrPass)
                    .addComponent(txtCurrPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(lblNewPass)
                    .addComponent(txtNewPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(txtConfimPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(lblConfimPass)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addComponent(lblMs)
                .addGap(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void mnChangeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnChangeMouseClicked
// TODO add your handling code here:
    Change_Pass();
}//GEN-LAST:event_mnChangeMouseClicked

private void mnCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnCloseMouseClicked
// TODO add your handling code here:
     int Close=JOptionPane.showConfirmDialog(null, "Are you sure want close  !");
       if(Close==0)
       this.dispose();
}//GEN-LAST:event_mnCloseMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Change_pass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Change_pass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Change_pass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Change_pass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Change_pass().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JLabel lblConfimPass;
    private javax.swing.JLabel lblCurrPass;
    private javax.swing.JLabel lblMs;
    private javax.swing.JLabel lblNewPass;
    private javax.swing.JMenu mnChange;
    private javax.swing.JMenu mnClose;
    private javax.swing.JPasswordField txtConfimPass;
    private javax.swing.JPasswordField txtCurrPass;
    private javax.swing.JPasswordField txtNewPass;
    // End of variables declaration//GEN-END:variables
}
